// pages/search/search.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    history: [], // 历史记录
    hots: [], // 热门搜索
    keywords: '', // 关键词
    schoolId: '', // 校区
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.schoolId){
      console.log(options.schoolId)
      this.setData({
        schoolId: options.schoolId
      })
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData();
  },

  // 获取数据
  getData(){
    app.loading();
    let that = this;
    wx.request({
      url: app.baseURL + 'index/keyword_list',
      data: {
        school_id: this.data.schoolId,
        token: wx.getStorageSync('userId') ? wx.getStorageSync('userId') : '',
      },
      success(data){
        let res = data.data;
        wx.hideLoading();
        if(res.code == 1){
          that.setData({
            history: res.data.history,
            hots: res.data.hotkey,
          })
        }else{
          app.$tip(res.msg)
        }
      }
    })
  },

  // 输入关键词
  initKey(e){
    this.setData({
      keywords: e.detail.value
    })
  },

  // 清空输入框
  clearAll(){
    this.setData({
      keywords: '',
    })
  },

  // 搜索按钮
  toResult(){
    if(!this.data.keywords){
      app.$tip('请输入您要搜索的商品!');
      return
    }

    if (this.data.schoolId){
      wx.navigateTo({
        url: '/pages/searchResult/searchResult?key=' + this.data.keywords + '&schoolId=' + this.data.schoolId,
      })
    }else{
      wx.navigateTo({
        url: '/pages/searchResult/searchResult?key='+this.data.keywords,
      })
    }
  },

  // 清空历史记录
  delHis(){
    let that = this;
    wx.showModal({
      title: '温馨提示',
      content: '您确定要清空历史记录吗?',
      confirmText: '清空',
      confirmColor: '#57B127',
      success(res){
        if(res.confirm){
          app.loading();
          wx.request({
            url: app.baseURL + 'index/delhistory',
            data: {
              token: app.globalData.userId
            },
            success(data){
              let res = data.data;
              wx.hideLoading();
              if(res.code == 1){
                app.$tip('清除成功!');
                setTimeout(() =>{
                  that.getData();
                },1500)
              }else{
                app.$tip(res.msg);
              }
            }
          })
        }
        if(res.cancel){

        }
      }
    })
  },

  // 选择当前key
  thisKey(e){
    this.setData({
      keywords: e.currentTarget.dataset.key,
    })

    if (this.data.schoolId){
      wx.navigateTo({
        url: '/pages/searchResult/searchResult?key=' + this.data.keywords + '&schoolId=' + this.data.schoolId,
      })
    }else{
      wx.navigateTo({
        url: '/pages/searchResult/searchResult?key='+this.data.keywords,
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})